package com.poul.acfun.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.poul.acfun.ac_model.bean.PushBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by poul on 2016/10/20.
 */

public class PushFoucusAdapter extends PagerAdapter{
    private Context context;
    private List<PushBean.DataBean.ContentsBean> list;
    private Pools.SimplePool<SimpleDraweeView> pools;

    public PushFoucusAdapter(Context context, List<PushBean.DataBean.ContentsBean> list) {
        this.context = context;
        this.list = new ArrayList<>(list);
        pools = new Pools.SimplePool<>(list.size());
        PushBean.DataBean.ContentsBean first = this.list.get(0);
        PushBean.DataBean.ContentsBean last = this.list.get(list.size() - 1);
        this.list.add(0,last);
        this.list.add(first);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        SimpleDraweeView img = pools.acquire();
        if (img == null){
            img = new SimpleDraweeView(context);
        }
        Uri uri = Uri.parse(list.get(position).getImage());
        img.setImageURI(uri);
        container.addView(img);
        return img;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pools.release((SimpleDraweeView) object);
    }
}
